package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.ExpertService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/experts")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    /**
     * 查询全部专家信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Expert> expertList = null;
        try {
            expertList = expertService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_QUERY_SUCCESS, expertList);
    }

    /**
     * 导出专家数据到 Excel表格
     * @param response
     * @param expert
     */
    @Log(title = "专家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Expert expert) {
        List<Expert> list = expertService.getList(expert);
        ExcelUtil<Expert> util = new ExcelUtil<Expert>(Expert.class);
        util.exportExcel(response, list, "专家数据");
    }

    /**
     * 分页获取专家信息
     * @param
     * @return
     */
    @GetMapping("/page")
    public PageResult getList(Expert expert){
        QueryPageUtils.startPage();
        List<Expert> expertList = null;
        PageInfo pageInfo;
        try {
            expertList = expertService.getList(expert);
            pageInfo = new PageInfo<>(expertList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.EXPERT_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.EXPERT_QUERY_SUCCESS, expertList);
    }

    /**
     * 根据专家id查询专家信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Expert byId = null;
        try {
            byId = expertService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_QUERY_SUCCESS, byId);
    }

    /**
     * 新增专家信息
     * @param expert
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Expert expert) {
        try {
            expertService.add(expert);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_ADD_SUCCESS);
    }

    /**
     * 专家头像上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/avatar")
    public Result avatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file); //路径 ->  /home/ruoyi/uploadPath/avatar
            return new Result(HttpStatusCode.SUCCESS, MessageConstant.UPLOAD_IMAGE_SUCCESS, avatar);
        }
        return new Result(HttpStatusCode.ERROR, MessageConstant.UPLOAD_IMAGE_FAIL);
    }

/*
* @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = getLoginUser();
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file); //  /home/ruoyi/uploadPath/avatar
            if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) { //如果修改成功
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                // 更新缓存用户头像
                loginUser.getUser().setAvatar(avatar);
                tokenService.setLoginUser(loginUser);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }*/
/*
* public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        String OriginalFileName = imgFile.getOriginalFilename();
        int i = OriginalFileName.lastIndexOf(".");
        String extention = OriginalFileName.substring(i - 1);//获得后缀 example：.jpg
        String fileName = UUID.randomUUID().toString() + extention;
        try {
            //将文件上传到七牛服务器
            FileUploadAndDeleteUtils.uploadToQiniu(imgFile.getBytes(),fileName);
            //将上传的图片名存到redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
*
* */

    /**
     * 修改专家信息
     * @param expert
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Expert expert){
        try {
            expertService.update(expert);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.EXPERT_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_UPDATE_SUCCESS);
    }

    /**
     * 根据专家id删除专家信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            expertService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_DELETE_SUCCESS);
    }
}
